<?php if ( !defined('BASEPATH'))     exit('No direct script access allowed');

/**
 * 
 * Author DaiT
 * Description This model helps us working with 'catalog' table
 * 
 */
class Model_catalog extends CI_Model {
           
    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    /**
     * 
     * Author DaiT
     * Description This function return table we want to work with. 
     *              It's useful when you change table's name, then all you must
     *                  do is change value of $table in this function
     * @return string Table name
     * 
     */
    function get_table() {
        $table = 'catalog';
        return $table;
    }
    
    /**
     * 
     * Author DaiT
     * Description This function get all ordered data from 'catalog' table
     * @param $start_row Where we want to start getting
     * @param $num_row Number of rows you want to get
     * @param $order_by Column we use to order
     *                  use NULL to get all
     * @param $order_option "asc" or "desc"
     * @return all data from 'catalog' table, which were ordered by $order_by
     * 
     */
    function get($start_row, $num_row, $order_by, $order_option) {
        $table = $this->get_table();
        $this->db->order_by($order_by, $order_option);
        
        if (isset($num_row)) {
            if (isset($start_row)) {
                $this->db->limit($num_row, $start_row);
            } else {
                $this->db->limit($num_row);
            }
        }
        
        $query = $this->db->get($table);
        return $query->result();
    }
    
    /**
     * 
     * Author DaiT
     * Description This function return records which satisfies $condition, start
     *              from $start_row and get maximum $num_row record(s)
     *              It's can use for paging
     * @param $conditions Array of conditions in format
     *                        array('id' => 3, 'name' => 'SMT', ...)
     *                     you can use other operatores, not only equal '='
     *                        array('id <' => 3, 'id%2' => 0 ...)
     *                     This way will AND all conditions. If you want to use
     *                          OR you must write WHERE clause in specified way
     *                         array('id = 3 OR id = 5')
     * @param $start_row Which row you want to start select, min value is 0
     * @param $num_row Number of records you want to get
     *                  Use 0 to get all record
     * @return Records satisfies $condition, start from $start_row and get maximum
     *              $num_row records.
     * 
     */
    function getWithConditions($conditions, $start_row, $num_row) {
        $table = $this->get_table();
        
        // the third parameter allow CI or not to protect fields by using backticks
        // Read CI User guild to get more infomation
        $this->db->where($conditions, NULL, FALSE);
        
        if (isset($num_row)) {
            if (isset($start_row)) {
                $this->db->limit($num_row, $start_row);
            } else {
                $this->db->limit($num_row);
            }
        }
        
        $query = $this->db->get($table);
        return $query->result();
    }
    
    /**
     * 
     * Author DaiT
     * Description Insert one or more record into database
     *              $data is in format
     *                  $data = array(
     *                      array(
     *                          'name' => 'shopgoox',
     *                          'status' => '1'
     *                          ...
     *                      ),
     *                      array(
     *                          'name' => 'smt',
     *                          'status' => '1',
     *                          ...
     *                      )
     *                  );
     * @param $data Data you want to insert
     * @return Number of rows were inserted
     * 
     */
    function insert($data) {
        $table = $this->get_table();
        
        $result = $this->db->insert_batch($table, $data);
        return $result;
    }
    
    /**
     * 
     * Author DaiT
     * Description This function use for updating one or more records
     * @param $data Data you want to update, include data of column use as the base
     * @param $base_column Name of the column use as the base
     * @return Number of rows were updated
     * 
     * Example: If you want to set 'title'='shopgoox' where 'id'=3 and where
     *          'id'=5 set 'title'='smt'
     *              Your $data = array(
     *                              array(
     *                                  'id' => 3,
     *                                  'title' => 'shopgoox'
     *                              ),
     *                              array(
     *                                  'id' => 5,
     *                                  'title' => 'smt'
     *                              )
     *                           );
     *              Then call update function
     *                  update($data, 'id')
     * 
     */
    function update($data, $base_column) {
        $table = $this->get_table();
        
        $result = $this->db->update_batch($table, $data, $base_column);
        return $result;
    }
}

